﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Util.Setting;

namespace thousandClear.Areas.Admin.Controllers
{
    public class MessageController : BaseController
    {
        // GET: Admin/Message
        public ActionResult Index(int? page, int? count, string status, string name)
        {
            AppSetting setting = new AppSetting();
            string level = "1";
            level = GetCookie("level");
            ViewBag.level = level;
            ViewBag.URL = setting.AppDomainName + "/statics/upload/Message/image/";
            page = page ?? 1;
            count = count ?? 10;
            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page.Value;
            int pageCount = count.Value;
            List<T_Message_Board> list = new List<T_Message_Board>();
            using (var db = new qianjingEntities())
            {
                var query = db.T_Message_Board.AsQueryable().Where(a => a.RowStatus != 2);
                if (!string.IsNullOrEmpty(status))
                {
                    query = query.Where(a => a.RowStatus == Convert.ToInt16(status));
                }

                if (!string.IsNullOrEmpty(name))
                {
                    var customer = db.O_Customer.FirstOrDefault(a => a.ocNicName == name);
                    if (customer != null)
                    {
                        query = query.Where(a => a.tmbOCID == customer.OCID);
                    }
                }
                totalCount = query.Count();
                var records = query.OrderByDescending(a => a.TMBID).Skip((page.Value - 1) * count.Value).Take(count.Value).ToList();
                foreach (var item in records)
                {
                    var csm = db.O_Customer.FirstOrDefault(a => a.OCID == item.tmbOCID);
                    list.Add(new T_Message_Board
                    {
                        TMBID = item.TMBID,
                        tmbBack = item.tmbBack,
                        tmbDesc = item.tmbDesc,
                        RowStatus = item.RowStatus,
                        CreatedDate = item.CreatedDate,
                        tmbImg1 = item.tmbImg1 == null ? "" : item.tmbImg1,
                        tmbImg2 = item.tmbImg2 == null ? "" : item.tmbImg2,
                        tmbImg3 = item.tmbImg3 == null ? "" : item.tmbImg3,
                        tmbImg4 = item.tmbImg4 == null ? "" : item.tmbImg4,
                        tmbImg5 = item.tmbImg5 == null ? "" : item.tmbImg5,
                        tmbImg6 = item.tmbImg6 == null ? "" : item.tmbImg6,
                        tmbImg7 = item.tmbImg7 == null ? "" : item.tmbImg7,
                        tmbImg8 = item.tmbImg8 == null ? "" : item.tmbImg8,
                        tmbImg9 = item.tmbImg9 == null ? "" : item.tmbImg9,
                        CreatedBy = csm.ocNicName,
                    });


                }


            }


            ViewBag.TotalCount = totalCount;
            totalPage = int.Parse((totalCount / count).ToString()) + (totalCount % count == 0 ? 0 : 1);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            ViewBag.PageCount = pageCount;

            return View(list);
        }

        [HttpGet]
        public void back(int TMBID, string BackDetail)
        {
            using (var db = new qianjingEntities())
            {
                var query = db.T_Message_Board.FirstOrDefault(a => a.TMBID == TMBID);
                query.tmbBack = BackDetail;
                query.RowStatus = 3;
                db.SaveChanges();
            }
        }


        [HttpGet]
        public void remove(long id)
        {
            using (var db = new qianjingEntities())
            {
                var query = db.T_Message_Board.FirstOrDefault(a => a.TMBID == id);

                query.RowStatus = 2;

                db.SaveChanges();
            }
        }
    }
}